﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Revit.Electric.Common;

namespace Model
{
	public class CableTraySystemType : HYCmdEntry
	{
		public CableTraySystemType(ExternalCommandData commandData) : base(commandData)
		{
		}

		public bool Excute()
		{
			Transaction transaction = null;
			bool result;
			try
			{
				transaction = new Transaction(this.m_doc, "type");
				transaction.Start();
				Reference reference = this.m_sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new RevitFilter.SelectionCableTrayFilter(), "请选择要编辑系统类型的桥架");
				CableTray cableTray = this.m_doc.GetElement(reference) as CableTray;
				DlgCableTraySystemType dlgCableTraySystemType = new DlgCableTraySystemType(cableTray);
				DialogResult dialogResult = dlgCableTraySystemType.ShowDialog();
				if (DialogResult.OK == dialogResult)
				{
					if (!dlgCableTraySystemType.chkApplied.Checked)
					{
						cableTray.get_Parameter(BuiltInParameter.RBS_CTC_SERVICE_TYPE).Set(dlgCableTraySystemType.cmbSystemType.Text);
					}
					else
					{
						foreach (Element element in new PathwayCheck().GetPathwayDevices(cableTray, SearchDeviceType.CableTray | SearchDeviceType.CableTrayFitting))
						{
							element.get_Parameter(BuiltInParameter.RBS_CTC_SERVICE_TYPE).Set(dlgCableTraySystemType.cmbSystemType.Text);
						}
					}
					transaction.Commit();
					result = true;
				}
				else
				{
					transaction.RollBack();
					result = false;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				transaction.Commit();
				result = false;
			}
			return result;
		}
	}
}
